package com.sskj.one.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.one.R;
import com.sskj.one.bean.GongGaoMsgBean;
import com.zzhoujay.richtext.RichText;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/05/24
 */
public class DetailGonggaoActivity extends BaseActivity<DetailGonggaoPresenter> {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tv_title_top)
    TextView tvTitleTop;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.tv_content)
    TextView tvContent;

    private String id;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_detail_gonggao;
    }

    @Override
    public DetailGonggaoPresenter getPresenter() {
        return new DetailGonggaoPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("公告详情");
        id = getIntent().getStringExtra("id");
    }

    @Override
    public void initData() {
        mPresenter.getGonggao(id);

    }

    public static void start(Context context, String id) {
        Intent intent = new Intent(context, DetailGonggaoActivity.class);
        intent.putExtra("id", id);
        context.startActivity(intent);
    }

    public void getGongGaoOK(GongGaoMsgBean data) {
        if (data == null) {
            return;
        }
        tvTitleTop.setText(data.getTitle());
        tvDate.setText(data.getTime());

        RichText.fromHtml(formatImg(data.getContent())).into(tvContent);
    }

    private String formatImg(String law_agree) {
        Document document = Jsoup.parse(law_agree);
        Elements imgs = document.getElementsByTag("img");
        for (Element img : imgs) {
            img.attr("width", "100%").attr("height", "auto");
            String url = img.attr("src");
            if (!url.contains("http")) {
                url = BaseHttpConfig.BASE_URL + url;
            }
            img.attr("src", url);
        }
        return document.toString();
    }
}
